<script setup>
import { MYURL } from '@/utils/express/express.js'
import { ref, watch, inject, nextTick, onBeforeUnmount } from 'vue'
import Vrouter from "@/router"
import { getChat, getChatUserInfo } from '@/api/chat/chat.js'
import { userStore } from '@/stores/user.js'
import { emojiDecode } from '@/utils/emoji/emoji.js'
const MyStore = userStore()
let send
let socket
nextTick(() => {
    socket = MyStore.socket
    socket.on('receive', (res, callback) => {
        chatMessage.value.push({
            message: res,
            time: Date.now(),
            myself: false
        })
        callback()
        nextTick(() => {
            window.scrollTo(0, document.documentElement.scrollHeight - document.documentElement.clientHeight);
        })
    })
    socket.on('send-success', (res) => {
        comment_text.value = ''
        chatMessage.value.push({
            message: res,
            time: Date.now(),
            myself: true
        })
        nextTick(() => {
            window.scrollTo(0, document.documentElement.scrollHeight - document.documentElement.clientHeight);
        })
    })
    socket.emit('clear-chat-num', search_text.value)
    socket.on('send-err', res => {
        success_message(res)
    })
    send = () => {
        if (commentEpty.value) return success_message('请输入信息')
        socket.emit('send', {
            to: search_text.value,
            message: comment_text.value
        })
    }
})
onBeforeUnmount(() => {
    socket.removeAllListeners('receive')
    socket.removeAllListeners('send-err')
    socket.removeAllListeners('send-success')
})
const router = Vrouter
let imgURL = `${MYURL}/public/icon/back_black.png`
const show_about = ref(false)
const commentEpty = ref(true)
const comment_text = ref('')
let reg = /[\t\r\f\n\s]*/g;


watch(comment_text, (e) => {
    commentEpty.value = e.replace(reg, '') == ''
})
const search_text = ref(router.currentRoute.value.query.id)
const userInfo = ref({})
const info_real = ref(false)
getChatUserInfo(router.currentRoute.value.query.id).then(res => {
    const message = res.data.message
    if (res.data.status === 0) {
        userInfo.value.myInfo = message.myInfo
        userInfo.value.anatherInfo = message.anatherInfo
        info_real.value = true
    } else {
        info_real.value = false
    }
})

const success_message = inject('change_message_state')

const chatMessage = ref([])
getChat(search_text.value).then((res) => {
    chatMessage.value = res.data.message
    nextTick(() => {
        window.scrollTo(0, document.documentElement.scrollHeight - document.documentElement.clientHeight);
    })
})
const my_input = ref(null)
const show_send = () => {
    nextTick(() => {
        my_input.value.focus()
    })
}
</script>


<template>
    <div v-if="!info_real">
        <isEmpty>没有这个人呀~</isEmpty>
    </div>
    <div v-if="info_real">
        <div class="back-ground"></div>
        <div class="get-back-main" ref="get_back_main">
            <img class="back-image" ref="back_image" :src="imgURL" @click="router.go(-1)">
            <div class="title">
                <p>{{ userInfo.anatherInfo[0].username }}</p>
            </div>
        </div>

        <div class="message-main">
            <div v-for="message, index in chatMessage" :key="index" :class="{ message_reverse: message.myself }"
                class="message-single">
                <img :src="MYURL + '/public/profiles/' + (message.myself ? userInfo.myInfo[0].profile : userInfo.anatherInfo[0].profile)"
                    alt="">
                <div>
                    <p>{{ emojiDecode(message.message) }}</p>
                </div>
            </div>
        </div>
        <div style="height: 60px;"></div>
        <div class="send_comment">

            <div class="send_btn">
                <div @click="show_send" class="btn_input">
                    <!-- <p :class="{ black: !commentEpty }"> {{ commentEpty ? '发个消息聊聊呗~' : comment_text }}</p> -->
                    <input ref="comment_input" type="text" class="comment_input" placeholder="发个消息聊聊呗~"
                        v-model="comment_text">
                </div>
                <p @click="send()">发送</p>
            </div>
            <div class="show_about" v-show="show_about">
                <div>
                    <textarea @input="my_about" ref="my_input" v-model="comment_text" name="" maxlength="200" id=""
                        cols="30" rows="10"></textarea>
                    <!-- <p class="text_count">{{ now_count }}</p> -->
                    <p class="sure_btn" @click="show_about = false">确定</p>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="less" scoped>
.comment_input {
    width: 100%;
    height: 100%;
    border: none;
    border-radius: 40px;
    padding: 0 0 0 20px;
    background-color: rgba(0, 0, 0, .01);
    outline: none;
}

.get-back-main {
    position: sticky;
}

.back-ground {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #f2f3f5;
    width: 100vw;
    height: 100vh;
    z-index: -1;
}

.black {
    color: black !important;
}

.back-image {
    width: 25px;
}

.send_btn {
    background-color: white;
    border-top: 1px solid rgba(0, 0, 0, .09);
    position: fixed;
    padding: 10px 10px;
    bottom: 0;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;


    .btn_input {
        width: 75%;
        height: 30px;
        background-color: #f6f7f8;
        border-radius: 12px;
        display: flex;
        align-items: center;

        p {
            color: #cacdd0;
            margin-left: 20px;
            font-size: 14px;
            overflow: hidden;
            padding-right: 10px;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
        }
    }

    >p {
        background-color: var(--bilibili-pink);
        color: white;
        border-radius: 15px;
        font-size: 13px;
        padding: 5px 15px;
    }
}

.show_about {
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.70);

    div {
        // border-radius: 100px;
        position: absolute;
        width: 100vw;
        height: 50vh;
        left: 50%;
        top: 30%;
        transform: translate(-50%, -50%);
        background-color: white;
        border-radius: 10px;

        textarea {
            border-radius: 10px;
            padding: 5px;
            font-size: 16px;
            width: 100%;
            height: 70%;
            border: none;
            outline: none;
            resize: none;
        }

        .text_count {
            position: absolute;
            color: #9a9a9a;
            right: 2%;
            bottom: 2%;
        }

        .sure_btn {
            background-color: var(--bilibili-pink);
            position: absolute;
            color: white;
            padding: 5px;
            border-radius: 10px;
            left: 2%;
            bottom: 2%;
        }
    }
}

.message_reverse {
    flex-direction: row-reverse;
}

.message-single {
    margin-top: 30px;
    display: flex;
    gap: 5px;


    >img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        object-fit: cover;
    }

    >div {
        max-width: 60vw;
        background-color: white;
        padding: 10px;
        border-radius: 10px;
    }
}
</style>
